package collector

import (
	"database/sql"
	"log/slog"
	"sync"

	"github.com/prometheus/client_golang/prometheus"
)

var (
	scrapers = make(map[string]Scraper)
	mutex    = &sync.RWMutex{}
)

// RegisterScraper adds a scraper to the registry.
func RegisterScraper(name string, scraper Scraper) {
	mutex.Lock()
	defer mutex.Unlock()
	scrapers[name] = scraper
}

// GetScrapers returns a list of all registered scrapers.
func GetScrapers() map[string]Scraper {
	mutex.RLock()
	defer mutex.RUnlock()
	// Create a copy to avoid race conditions.
	list := make(map[string]Scraper, len(scrapers))
	for name, scraper := range scrapers {
		list[name] = scraper
	}
	return list
}

// Scraper is minimal interface that let's you add new prometheus metrics to dm_exporter.
type Scraper interface {
	// Name of the Scraper. Should be unique.
	Name() string

	// Help describes the role of the Scraper.
	// Example: "Collect from SHOW ENGINE INNODB STATUS"
	//Help() string

	// Scrape collects data from client and sends it over channel as prometheus metric.
	Scrape(db *sql.DB, ch chan<- prometheus.Metric, backupPath string, logger *slog.Logger) error
}
